package com.zmn.plat.admin.controller.standard.record;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.office.excel.ExportExcel;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.mcc.cas.web.StaffAdminService;
import com.zmn.plat.common.dictionary.StandardCategoryEnum;
import com.zmn.plat.model.dto.common.TreeGridDTO;
import com.zmn.plat.model.entity.standard.read.permission.OrgPermitTreeVO;
import com.zmn.plat.model.entity.standard.record.StandardRecord;
import com.zmn.plat.model.entity.standard.record.StandardRecordQuery;
import com.zmn.plat.model.vo.standard.record.StandardRecordExportExcelVO;

/**
 * 标准列表
 *
 * @author linfeng
 * @since 2020-02-12 10:23:38
 */
@Controller
@RequestMapping("/standard/record/")
public class StandardRecordListController extends StandardRecordBaseController {

    /**
     * 列表
     *
     * @param query
     * @author linfeng
     * @since 2020-02-12 10:23:38
     */
    @RequestMapping("list")
    @RequiresPermissions("/standard/record/list.action")
    public ModelAndView list(@ModelAttribute StandardRecordQuery query) {
        query.setStaffId(getMcStaff().getStaffId());
        query.setCreater(getMcStaff().getRealName());
        List<StandardRecord> items = standardRecordService.listPageByQuery(query);

        boolean showExportExcel = StaffAdminService.isPermitted("/standard/record/exportExcel.action");

        ModelAndView mav = new ModelAndView("standard/record/list");
        mav.addObject("items", items);
        mav.addObject("categorys", StandardCategoryEnum.values());
        mav.addObject("vo", query);
        mav.addObject("oneDepts", getOneDeptList());
        mav.addObject("twoDepts", getTwoDeptList());
        mav.addObject("showExportExcel", showExportExcel);
        return mav;
    }

    /**
     * 查看标准规范列表
     *
     * @param query
     * @author linfeng
     * @since 2020-02-12 10:23:38
     */
    @RequestMapping("checkList")
    // @RequiresPermissions("/standard/record/checkList.action")
    public ModelAndView checkList(@ModelAttribute StandardRecordQuery query) {
        query.setStatus(GlobalConsts.YES);
        query.setStaffId(getMcStaff().getStaffId());
        query.setCreater(getMcStaff().getRealName());
        List<StandardRecord> items = standardRecordService.listPageByCheckQuery(query);

        ModelAndView mav = new ModelAndView("standard/record/check/list");
        mav.addObject("items", items);
        mav.addObject("categorys", StandardCategoryEnum.values());
        mav.addObject("vo", query);
        return mav;
    }

    @RequestMapping("listAjax")
    @ResponseBody
    public ResponseDTO listAjax(StandardRecordQuery query) {
        query.setPageSize(8);
        query.setStatus(GlobalConsts.YES);
        query.setStaffId(getMcStaff().getStaffId());
        query.setCreater(getMcStaff().getRealName());
        // 关闭查询时间过滤 非默认查询一个月
        query.setCloseQueryTimeFilter(true);
        List<StandardRecord> items = standardRecordService.listPageByQuery(query);
        return ResponseDTO.success(items);
    }

    /**
     * 详情
     *
     * @param standardId
     * @author linfeng
     * @since 2020-02-12 10:23:38
     */
    @RequestMapping("detail")
    @RequiresPermissions("/standard/record/detail.action")
    public ModelAndView detail(@RequestParam(value = "standardId", defaultValue = "0") Integer standardId) {
        StandardRecord standardRecord;
        if (standardId == 0) {
            standardRecord = new StandardRecord();
        } else {
            standardRecord = standardRecordService.findByKey(standardId);
        }

        ModelAndView mav = new ModelAndView("standard/record/detail");
        mav.addObject("standardRecord", standardRecord);
        mav.addObject("categorys", StandardCategoryEnum.values());
        mav.addObject("oneDepts", getOneDeptList());
        mav.addObject("twoDepts", getTwoDeptList());
        mav.addObject("flag", 1);
        return mav;
    }

    @RequestMapping(value = "content")
    @RequiresPermissions("/standard/record/content.action")
    public ModelAndView content(@RequestParam(value = "standardId", defaultValue = "0") Integer standardId) {
        StandardRecord standardRecord;
        if (standardId == 0) {
            standardRecord = new StandardRecord();
        } else {
            standardRecord = standardRecordService.findByKey(standardId);
        }

        ModelAndView modelAndView = new ModelAndView("standard/record/content");
        modelAndView.addObject("standardRecord", standardRecord);
        modelAndView.addObject("flag", 2);
        return modelAndView;
    }

    @RequestMapping(value = "preview")
    public ModelAndView preview(@RequestParam(value = "standardId", defaultValue = "0") Integer standardId,
        @RequestParam(value = "flag", defaultValue = "0") Integer flag) {
        StandardRecord standardRecord = standardRecordService.findByKey(standardId);
        // 跳转到具体的运营后台文章详情页面则次数加一
        standardRecordService.incrReadCount(standardId);
        ModelAndView modelAndView = new ModelAndView("standard/record/preview");
        modelAndView.addObject("standardRecord", standardRecord);
        modelAndView.addObject("flag", flag);
        return modelAndView;
    }

    /**
     * 标准列表阅读权限
     *
     * @param standardId
     * @author xiangjunming
     * @since 2020-02-12 10:23:38
     */
    @RequestMapping("permission")
    @RequiresPermissions("/standard/record/permission.action")
    public ModelAndView permission(@RequestParam(value = "standardId", defaultValue = "0") Integer standardId) {
        StandardRecord standardRecord = standardRecordService.findByKey(standardId);
        ModelAndView mav = new ModelAndView("standard/record/permission");
        mav.addObject("standardRecord", standardRecord);
        mav.addObject("flag", 3);
        return mav;
    }

    /**
     * 标准阅读权限列表
     *
     * @param standardId
     * @return
     */
    @RequestMapping(value = "permission/tree")
    public void tree(HttpServletRequest request, HttpServletResponse response, Integer standardId) {
        List<OrgPermitTreeVO> list = standardReadPermissionService.listOrgPermitTreeVOByStandardId(standardId);
        // 封装权限树返回JSON
        TreeGridDTO treeGrid = new TreeGridDTO();
        treeGrid.setData(list);
        treeGrid.setCount(list != null ? list.size() : 0);
        ResponseUtil.outWriteJson(response, JSON.toJSONString(treeGrid));
    }

    /**
     * 根据员工ID获取未阅读的标准
     *
     * @return
     */
    @RequestMapping(value = "listByUnReadAndStaffId")
    @ResponseBody
    public ResponseDTO listByUnReadAndStaffId() {
        int staffId = getMcStaff().getStaffId();
        List<StandardRecord> standardRecords = standardRecordService.listByUnReadAndStaffId(staffId);
        return ResponseDTO.success(standardRecords);
    }

    @RequestMapping(value = "exportExcel")
    @RequiresPermissions("/standard/record/exportExcel.action")
    public void exportExcel(HttpServletResponse response, StandardRecordQuery query) {
        Integer pageSize = getExportPageLimit(response);
        query.setPageSize(pageSize);

        try {
            ExportExcel<StandardRecordExportExcelVO> excel = standardRecordBService.exportExcel(query, getMcStaff());
            excel.exportExcel(response, "标准文库管理列表");
        } catch (Exception e) {
            logger.error("导出报表出错" + e.getMessage(), e);
        }
    }

    @RequestMapping(value = "downLoadFiles")
    @RequiresPermissions("/standard/record/downLoadFiles.action")
    public void downLoadFiles(HttpServletResponse response, Integer standardId) {
        standardRecordBService.downLoadFiles(response, standardId);
    }

    @RequestMapping(value = "downLoad")
    @RequiresPermissions("/standard/record/downLoad.action")
    public void downLoadFile(HttpServletResponse response, String file) {
        standardRecordBService.downLoadFile(response, file);
    }
}
